﻿using IronPython.Runtime.Operations;
using Microsoft.CodeAnalysis.FlowAnalysis.DataFlow.CopyAnalysis;
using System;

namespace CoreLims.Models.CoreLims
{
    public partial class Eq
    {
        public static EqEquipment Equipment;
        public static EqEquipmentInfo EquipmentInfo;
        public static EqUsageRecord UsageRecord;
        public static EqMaintain Maintain;
        public static EqMaintainEvent MaintainEvent;
    }

    /// <summary>
    /// 设备
    /// </summary>
    public partial class EqEquipment
    {
        public EqEquipment()
        {
            this.Status = "启用";
        }

        /// <summary>
        /// 设备编号（实验室内部编号）
        /// </summary>
        public string Id { get; set; }

        /// <summary>
        /// 设备名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 设备类型
        /// </summary>
        public string Type { get; set; }

        /// <summary>
        /// 状态。启用/停用/报废
        /// </summary>
        public string Status { get; set; }

        /// <summary>
        /// 制造商
        /// </summary>
        public string Manufacturer { get; set; }

        /// <summary>
        /// 型号
        /// </summary>
        public string Model { get; set; }

        /// <summary>
        /// 实验室
        /// </summary>
        public string Site { get; set; }

        /// <summary>
        /// 所属工作组
        /// </summary>
        public string Group { get; set; }

        /// <summary>
        /// 关联系统的供应商ID
        /// </summary>
        public string SupplierId { get; set; }

        /// <summary>
        /// 供应商名称
        /// </summary>
        public string SupplierName { get; set; }

        /// <summary>
        /// 出厂编号
        /// </summary>
        public string FactoryNumber { get; set; }

        /// <summary>
        /// 安装位置
        /// </summary>
        public string InstallationLocation { get; set; }

        /// <summary>
        /// 启用日期
        /// </summary>
        public DateTime? EnablingDate { get; set; }

        /// <summary>
        /// 购入日期
        /// </summary>
        public DateTime? PurchaseDate { get; set; }

        /// <summary>
        /// 负责人
        /// </summary>
        public string ResponsiblePerson { get; set; }

        /// <summary>
        /// 负责人ID
        /// </summary>
        public string ResponsiblePersonId { get; set; }

        public void Copy(EqEquipment other)
        {
            this.Name = other.Name;
            this.Type = other.Type;
            this.Manufacturer = other.Manufacturer;
            this.Model = other.Model;
            this.Site = other.Site;
            this.Group = other.Group;
            this.SupplierId = other.SupplierId;
            this.SupplierName = other.SupplierName;
            this.FactoryNumber = other.FactoryNumber;
            this.InstallationLocation = other.InstallationLocation;
            this.EnablingDate = other.EnablingDate;
            this.PurchaseDate = other.PurchaseDate;
            this.ResponsiblePersonId = other.ResponsiblePersonId;
            this.ResponsiblePerson = other.ResponsiblePerson;
        }
    }

    /// <summary>
    /// 设备扩展信息表，根据具体实施情况可以自定义的扩展信息
    /// </summary>
    public partial class EqEquipmentInfo
    {
        /// <summary>
        /// 关联设备编号
        /// </summary>
        public string EquipmentId { get; set; }

    }

    /// <summary>
    /// 设备使用记录
    /// </summary>
    public partial class EqUsageRecord
    {
        public EqUsageRecord()
        {
            this.Id = Guid.NewGuid().ToString();
            this.RecordTime = DateTime.Now;
        }

        /// <summary>
        /// 运行ID。一台设备一次测试的所有样品使用一个ID。
        /// </summary>
        public string Id { get; set; }

        /// <summary>
        /// 设备ID
        /// </summary>
        public string EqId { get; set; }

        /// <summary>
        /// 设备名称
        /// </summary>
        public string EqName { get; set; }

        /// <summary>
        /// 设备类型
        /// </summary>
        public string EqType { get; set; }

        /// <summary>
        /// 导入数据时，该ID指定上传的仪器数据文件
        /// </summary>
        public string FileID { get; set; }

        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime? StartTime { get; set; }

        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime? EndTime { get; set; }

        /// <summary>
        /// 记录登记时间
        /// </summary>
        public DateTime RecordTime { get; set; }

        /// <summary>
        /// 时长使用小时为计量单位。
        /// 不要直接设置该值，使用 SetDuration() 函数计算该值
        /// </summary>
        public decimal? Duration { get; set; }

        /// <summary>
        /// 使用人ID
        /// </summary>
        public string PersonId { get; set; }

        /// <summary>
        /// 使用人姓名
        /// </summary>
        public string PersonName { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        public string Note { get; set; }

        /// <summary>
        /// 计算时差，成功返回true。如果时差小于0，则不赋值
        /// </summary>
        /// <returns></returns>
        public bool SetDuration()
        {
            bool result = false;
            if (StartTime != null && EndTime != null)
            {
                var temp = ((DateTime)EndTime - (DateTime)StartTime).TotalHours.ToString("f1");
                decimal a = 0;
                decimal.TryParse(temp, out a);

                if (a > 0)
                {
                    Duration = a;
                    result = true;
                }
            }
            return result;
        }
    }

    /// <summary>
    /// 设备维护记录
    /// </summary>
    public partial class EqMaintain
    {
        public EqMaintain()
        {
            this.Id = Guid.NewGuid().ToString();
            this.RecordTime = DateTime.Now;
        }

        public string Id { get; set; }

        /// <summary>
        /// 设备ID
        /// </summary>
        public string EqId { get; set; }

        /// <summary>
        /// 对应的事件ID
        /// </summary>
        public string MaintainEventId { get; set; }

        /// <summary>
        /// 维护说明
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime? StarTime { get; set; }

        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime? EndTime { get; set; }

        /// <summary>
        /// 当前记录产生的时间
        /// </summary>
        public DateTime RecordTime { get; set; }

        /// <summary>
        /// 维护人ID
        /// </summary>
        public string PersonId { get; set; }

        /// <summary>
        /// 维护人姓名
        /// </summary>
        public string PersonName { get; set; }

    }

    /// <summary>
    /// 设备维护事件
    /// </summary>
    public partial class EqMaintainEvent
    {
        public EqMaintainEvent()
        {
            this.Id = Guid.NewGuid().ToString();
            this.Frequency = 0;
            this.LastTime = DateTime.Now;
            this.Status = "停用";
            this.IsSendTo = false;
        }

        private int _Frequency;
        private string _FrequencyUnit;
        private DateTime _LastTime;

        public string Id { get; set; }

        /// <summary>
        /// 维护事件名称
        /// </summary>
        public string EventsName { get; set; }

        /// <summary>
        /// 描述
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// 关联的设备ID
        /// </summary>
        public string EqId { get; set; }

        /// <summary>
        /// 上次维护完成时间
        /// </summary>
        public DateTime LastTime {
            get { return this._LastTime; }
            set
            {
                this._LastTime = value;
                SetNextTime();
            }
        }

        /// <summary>
        /// 维护频率。
        /// </summary>
        public int Frequency {
            get { return this._Frequency; }
            set
            {
                this._Frequency = value;
                SetNextTime();
            } 
        }

        /// <summary>
        /// 维护频率单位。天、周、月、年
        /// </summary>
        public string FrequencyUnit {
            get { return this._FrequencyUnit; }
            set
            {
                this._FrequencyUnit = value;
                SetNextTime();
            }
        }

        /// <summary>
        /// 下次维护时间。根据频率和维护频率单位和上次维护完成时间，计算出该时间。
        /// </summary>
        public DateTime? NextTime { get; set; }

        /// <summary>
        /// 提前提醒的天数
        /// </summary>
        public int Remind { get; set; }

        /// <summary>
        /// 提醒消息接收组
        /// </summary>
        public string SendToGroup { get; set; }
        
        /// <summary>
        /// 提醒消息接收组ID
        /// </summary>
        public string SendToGroupId { get; set; }

        /// <summary>
        /// 提醒消息接收人，默认为设备负责人
        /// </summary>
        public string SendToPerson { get; set; }

        /// <summary>
        /// 提醒消息接收人ID
        /// </summary>
        public string SendToPersonId { get; set; }

        /// <summary>
        /// 事件状态，启用/停用
        /// </summary>
        public string Status { get; set; }

        /// <summary>
        /// 是否已发送消息提醒
        /// </summary>
        public bool IsSendTo { get; set; }

        private void SetNextTime()
        {
            if (this.Frequency == 0)
                return;
            if (this.FrequencyUnit is null)
                return;

            if (this.FrequencyUnit == "天")
                this.NextTime = this.LastTime.AddDays(this.Frequency);
            if (this.FrequencyUnit == "周")
                this.NextTime = this.LastTime.AddDays(this.Frequency * 7);
            if (this.FrequencyUnit == "月")
                this.NextTime = this.LastTime.AddMonths(this.Frequency);
            if (this.FrequencyUnit == "年")
                this.NextTime = this.LastTime.AddYears(this.Frequency);
        }
    }

    /// <summary>
    /// 该类是用来返回分析项包含的设备列表时使用，同样使用于返回文件列表。
    /// </summary>
    public class UseTypeList
    {
        /// <summary>
        /// 该值如果为 ture ，说明分析项包含该类型设备
        /// </summary>
        public bool Use { get; set; }

        /// <summary>
        /// 设备类型
        /// </summary>
        public string Type { get; set; }
    }
}
